import { createVNode, render } from 'vue'
import RbtConfirm from './rbt-confirm.vue'

// 准备div
const div = document.createElement('div')
div.setAttribute('class', 'rbt-confirm-container')
document.body.appendChild(div)

// 该函数渲染rbt-confirm组件 标题和文本
// 函数的返回值是promise对象
export default ({ title, text }: { title: string; text: string }) => {
  return new Promise<void>((resolve, reject) => {
    const submitCallback = () => {
      render(null, div)
      resolve()
    }
    const cancelCallback = () => {
      render(null, div)
      reject(new Error('点击取消'))
    }

    const vnode = createVNode(RbtConfirm, {
      title,
      text,
      submitCallback,
      cancelCallback
    })
    render(vnode, div)
  })
}
